/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.client;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * Binding part of the parts loader, registering necessary listeners for requests ({@link OPPartLoadingRequest}) and finished loading event, and for checking if a part is already loaded.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 08 January 2010
 */
public final class OPPartsLoaderBinding extends JavaScriptObject {

    /**
     * Returns a binding object to encapsulate an {@link OPPartsLoader}.
     * 
     * @return a binding object to encapsulate an OPPartsLoader. Cannot be null.
     */
    public static final native OPPartsLoaderBinding partsLoaderBindingFactory() /*-{
        var partsLoaderBinding = {};
        partsLoaderBinding.executeLoadingRequest = null;
        partsLoaderBinding.notifyLoadingCompleted = null;
        partsLoaderBinding.isPartLoaded = null;
        return partsLoaderBinding;
    }-*/;

    /**
     * Protected constructor, necessary because it's a JavaScriptObject.
     */
    protected OPPartsLoaderBinding() {
    }

    /**
     * Ask the parts loader to load the part described in the given loading request. Don't do anything if the parts loader hasn't been binded.
     * 
     * @param request
     *            the loading request to execute. Shouldn't be null.
     */
    public void executeLoadingRequest(final OPPartLoadingRequest request) {
        assert request != null : "parameter request shouldn't be null";

        executeLoadingRequestImpl(request);
    }

    /**
     * Real implementation of asking the parts loader to load the part described in the given loading request. Don't do anything if the parts loader hasn't been binded.
     * 
     * @param request
     *            the loading request to execute.
     */
    private final native void executeLoadingRequestImpl(final OPPartLoadingRequest request) /*-{
        if (this.executeLoadingRequest != null) {
            this.executeLoadingRequest(request);
        }
    }-*/;

    /**
     * Notify the part loader that the part with the given service name finished loading and binding itself. Don't do anything if the parts loader hasn't been
     * binded.
     * 
     * @param service
     *            the name of the service of the part that finished loading. Shouldn't be null.
     */
    public void notifyLoadingCompleted(final String service) {
        assert service != null : "parameter service shouldn't be null";

        notifyLoadingCompletedImpl(service);
    }

    /**
     * Real implementation of notifying the part loader that the part with the given service name finished loading and binding itself. Don't do anything if the
     * parts loader hasn't been binded.
     * 
     * @param service
     *            the name of the service of the part that finished loading.
     */
    private final native void notifyLoadingCompletedImpl(final String service) /*-{
        if (this.notifyLoadingCompleted != null) {
            this.notifyLoadingCompleted(service);
        }
    }-*/;

    
    /**
     * Ask the parts loader if the part with the given service name is already loaded. Return false if the parts loader hasn't been binded.
     * 
     * @param service
     *            the service name of the part to check for. Shouldn't be null.
     */
    public boolean isPartLoaded(final String service) {
        assert service != null : "parameter service shouldn't be null";

        return isPartLoadedImpl(service);
    }

    /**
     * Real implementation of asking the parts loader if the part with the given service name is already loaded. Return false if the parts loader hasn't been
     * binded.
     * 
     * @param service
     *            the service name of the part to check for.
     */
    private final native boolean isPartLoadedImpl(final String service) /*-{
        if (this.isPartLoaded != null) {
            return this.isPartLoaded(service);
        } else {
            return false;
        }
    }-*/;
    
}
